global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area

*******************************************************
* Replication for Figure E_68 in supplementary analysis
*******************************************************



use "$outputs/ES/paneldata_ES.dta", clear
g logE001=log(E001)
g logemp=log(E001)
g loghours=log(E012)
g logwage=log(wage_cap)
g logwagebill=log(D029)
g logca=log(D001)
g logtemp=log(E010)
g logEBT=log(D086)
g logparttime=log(E007)
g loginternal=log(MG002)
g logassets=log(B001)
g logservices=log(MG011)
g logturnoverinternal=log(MG001+MG002)
g logestabfor=log(numestabest)
g logtangible=log(B012)
g logcash=log(B049)

*drop firms that export to other EU market (no posting)
g export_other=0
replace export_other=1 if MG008>0
egen panel_export_other=sum(export_other), by(tina)
g alt_treat=0
replace alt_treat=1 if panel_export_other>=1
drop if alt_treat==1
drop cluster
egen cluster1=group(cae3 distrito)
egen cluster2=group(cae3 ano)
egen cluster3=group(cae3 dimcomissao)
*binning coefficients
g coeff_up=0
replace coeff_up=1 if treated==1 & time>6
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4

label var coeff_up ">6"
*drop if ano<2008
drop if event_d<2010 & treated==1
drop if event_d>2015 & treated==1

g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"

global depvar logassets E001 logemp loghours logwage logwagebill logca logEBT logparttime loginternal
label var coeff_bottom "-4<"

   egen median=median(E001), by(tina)
   drop if median<3
   drop if median==.
   
   g export_servicedum=export_service_EU 
   replace export_servicedum=0 if ano==event_d
   
   save "$outputs/fulldatasetforestimation.dta", replace
   
   *create a dataset with treated ID and their characteristics at the time of the event 
   
   use "$outputs/fulldatasetforestimation.dta", clear
   keep if treated==1
   keep tina cae3 distrito dimcomissao ancon ano time event_d treated D001
   foreach y in dimcomissao cae3 distrito ancon D001{
   g `y'd =`y'  if time==-1
   egen pre`y'=min(`y'd), by(tina)
   }
   
   collapse (mean) predimcomissao precae3 preancon predistrito preD001 event_d treated, by(tina) 
   g preevent=event_d-1
   save "$outputs/treatedIDallyears.dta", replace
   
   use "$outputs/fulldatasetforestimation.dta", clear
   keep if treated==0
   keep tina cae3 distrito dimcomissao ancon ano time event_d treated D001
   keep if ano>2008 & ano<2015
   save "$outputs/controlIDallyears.dta", replace
   
   
   
   *matching per year of pre event 
   
   use "$outputs/treatedIDallyears.dta", clear 
   keep if event_d==2010
   drop if missing(precae3)
   rename precae3 cae3
   rename predimcomissao dimcomissao
   rename preancon ancon
   rename predistrito distrito
   rename preevent ano
   rename preD001 D001
   append using "$outputs/controlIDallyears.dta"
   keep if ano==2009
   egen scoring=group(dimcomissao cae3 ancon distrito D001)
   psmatch2 treated, pscore(scoring) neighbor(1) common ties /*caliper(0.1)*/
   g flag=.
   replace flag=_id if missing(_n1)
   replace flag=_n1 if treated==1
   g flagi=1
   egen pair=sum(flagi), by(flag)
   *drop unmatched treated here 121
   drop if flag==. 
   order pair flag _id _n1 treated tina
   sort pair flag _id 
   *keeping all observations macthed 
   keep if pair>1
   *assigning placebo event
   replace event_d=2010 if treated==0
   save "$outputs/matched2010.dta", replace
   keep tina event_d treated
   save "$outputs/matched2010IDonly.dta", replace
   
   use "$outputs/treatedIDallyears.dta", clear 
   keep if event_d==2011
   drop if missing(precae3)
   rename precae3 cae3
   rename predimcomissao dimcomissao
   rename preancon ancon
   rename predistrito distrito
   rename preevent ano
   rename preD001 D001
   append using "$outputs/controlIDallyears.dta"
   keep if ano==2010
   merge 1:1 tina using "$outputs/matched2010IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   egen scoring=group(dimcomissao cae3 ancon distrito D001)
   psmatch2 treated, pscore(scoring) neighbor(1) common ties /*caliper(0.1)*/
   g flag=.
   replace flag=_id if missing(_n1)
   replace flag=_n1 if treated==1
   g flagi=1
   egen pair=sum(flagi), by(flag)
   *drop unmatched treated here 121
   drop if flag==.
   order pair flag _id _n1 treated tina
   sort pair flag _id 
   *keeping all observations macthed 
   keep if pair>1
   *assigning placebo event
   replace event_d=2011 if treated==0
   save "$outputs/matched2011.dta", replace
   keep tina event_d treated
   save "$outputs/matched2011IDonly.dta", replace
   
   use "$outputs/treatedIDallyears.dta", clear 
   keep if event_d==2012
   drop if missing(precae3)
   rename precae3 cae3
   rename predimcomissao dimcomissao
   rename preancon ancon
   rename predistrito distrito
   rename preevent ano
   rename preD001 D001
   append using "$outputs/controlIDallyears.dta"
   keep if ano==2011
   merge 1:1 tina using "$outputs/matched2010IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2011IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   egen scoring=group(dimcomissao cae3 ancon distrito D001)
   psmatch2 treated, pscore(scoring) neighbor(1) common ties /*caliper(0.1)*/
   g flag=.
   replace flag=_id if missing(_n1)
   replace flag=_n1 if treated==1
   g flagi=1
   egen pair=sum(flagi), by(flag)
   *drop unmatched treated here 121
   drop if flag==.
   order pair flag _id _n1 treated tina
   sort pair flag _id 
   *keeping all observations macthed 
   keep if pair>1
   *assigning placebo event
   replace event_d=2012 if treated==0
   save "$outputs/matched2012.dta", replace
   keep tina event_d treated
   save "$outputs/matched2012IDonly.dta", replace
   
   
    use "$outputs/treatedIDallyears.dta", clear 
   keep if event_d==2013
   drop if missing(precae3)
   rename precae3 cae3
   rename predimcomissao dimcomissao
   rename preancon ancon
   rename predistrito distrito
   rename preevent ano
   rename preD001 D001
   append using "$outputs/controlIDallyears.dta"
   keep if ano==2012
   merge 1:1 tina using "$outputs/matched2010IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2011IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2012IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   egen scoring=group(dimcomissao cae3 ancon distrito D001)
   psmatch2 treated, pscore(scoring) neighbor(1) common ties /*caliper(0.1)*/
   g flag=.
   replace flag=_id if missing(_n1)
   replace flag=_n1 if treated==1
   g flagi=1
   egen pair=sum(flagi), by(flag)
   *drop unmatched treated here 121
   drop if flag==.
   order pair flag _id _n1 treated tina
   sort pair flag _id 
   *keeping all observations macthed 
   keep if pair>1
   *assigning placebo event
   replace event_d=2013 if treated==0
   save "$outputs/matched2013.dta", replace
   keep tina event_d treated
   save "$outputs/matched2013IDonly.dta", replace
   
   use "$outputs/treatedIDallyears.dta", clear 
   keep if event_d==2014
   drop if missing(precae3)
   rename precae3 cae3
   rename predimcomissao dimcomissao
   rename preancon ancon
   rename predistrito distrito
   rename preevent ano
   rename preD001 D001
   append using "$outputs/controlIDallyears.dta"
   keep if ano==2013
   merge 1:1 tina using "$outputs/matched2010IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2011IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2012IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2013IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   egen scoring=group(dimcomissao cae3 ancon distrito D001)
   psmatch2 treated, pscore(scoring) neighbor(1) common ties /*caliper(0.1)*/
   g flag=.
   replace flag=_id if missing(_n1)
   replace flag=_n1 if treated==1
   g flagi=1
   egen pair=sum(flagi), by(flag)
   *drop unmatched treated here 121
   drop if flag==.
   order pair flag _id _n1 treated tina
   sort pair flag _id 
   *keeping all observations macthed 
   keep if pair>1
   *assigning placebo event
   replace event_d=2014 if treated==0
   save "$outputs/matched2014.dta", replace
   keep tina event_d treated
   save "$outputs/matched2014IDonly.dta", replace
   
   
   use "$outputs/treatedIDallyears.dta", clear 
   keep if event_d==2015
   drop if missing(precae3)
   rename precae3 cae3
   rename predimcomissao dimcomissao
   rename preancon ancon
   rename predistrito distrito
   rename preevent ano
   rename preD001 D001
   append using "$outputs/controlIDallyears.dta"
   keep if ano==2014
   merge 1:1 tina using "$outputs/matched2010IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2011IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2012IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2013IDonly.dta"
   drop if _m==3
   drop if _m==2
   drop _m
   merge 1:1 tina using "$outputs/matched2014IDonly.dta"
   drop if _m==3
   drop if _m==2
   egen scoring=group(dimcomissao cae3 ancon distrito D001)
   psmatch2 treated, pscore(scoring) neighbor(1) common ties /*caliper(0.1)*/
   g flag=.
   replace flag=_id if missing(_n1)
   replace flag=_n1 if treated==1
   g flagi=1
   egen pair=sum(flagi), by(flag)
   *drop unmatched treated here 121
   drop if flag==.
   order pair flag _id _n1 treated tina
   sort pair flag _id 
   *keeping all observations macthed 
   keep if pair>1
   *assigning placebo event
   replace event_d=2015 if treated==0
   save "$outputs/matched2015.dta", replace
   keep tina event_d treated
   save "$outputs/matched2015IDonly.dta", replace
   
   *****creating the panel of treated firms & their match******
   
   use "$outputs/matched2010IDonly.dta", clear
   append using "$outputs/matched2011IDonly.dta"
   append using "$outputs/matched2012IDonly.dta"
   append using "$outputs/matched2013IDonly.dta"
   append using "$outputs/matched2014IDonly.dta"
   append using "$outputs/matched2015IDonly.dta"
   rename treated treated_pan
   rename event_d event_pan
   save "$outputs/panelmatched20102015ID.dta", replace
   
   
   use "$outputs/fulldatasetforestimation.dta", clear
   merge m:1 tina using "$outputs/panelmatched20102015ID.dta"
   keep if _m==3
   drop time timed coeff*
   
   
   g time=ano-event_pan
   g timed=time+9 

forval y=0/16{
g coeff_`y'=0
replace coeff_`y'=1 if timed==`y'
}
   
  forval y=0/16{
g interac_`y'=0
replace interac_`y'=1 if timed==`y' & treated==1
} 
   
   
   
g coeff_bottom=0
replace coeff_bottom=1 if treated==1 & time<-4



g coeff_up2=0
replace coeff_up2=1 if time>4 & treated==1
label var coeff_up2 ">4"
  
 forval y=0/16{
local j=`y'-9
label var interac_`y' "`j'"
}    
 
 egen clusterb1=group(event_pan  distrito)
 
foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x' coeff_0-coeff_7  coeff_9-coeff_16  coeff_bottom interac_5-interac_7 zero interac_9-interac_13 coeff_up2, absorb( i.tina  i.ano##i.distrito##i.cae3 ) cluster(clusterb1)
outreg2 using "$outputs/ES/Baseline_Export5/Matching/Estimates/`x'_full_c`y'.txt",replace
coefplot, keep( coeff_bottom interac_5 interac_6 interac_7 zero interac_9 interac_10 interac_11 interac_12 interac_13 coeff_up2) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6)
graph export "$outputs/ES/Baseline_Export5/Matching/Figures/`x'_full_c`y'.png", replace
coefplot, keep(  interac_5 interac_6 interac_7 zero interac_9 interac_10 interac_11 interac_12 interac_13 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6)
graph export "$outputs/ES/Baseline_Export5/Matching/Figures/`x'_full_c`y'_cut.png", replace
}
}

   
  foreach x in logca logEBT logservices logassets logemp logwagebill loghours logwage loginternal{
forval y=1/1{
reghdfe `x'  coeff_bottom interac_5-interac_7 zero interac_9-interac_13 coeff_up2, absorb( i.tina  i.ano##i.distrito##i.cae3 ) cluster(clusterb1)

coefplot, keep(  interac_5 interac_6 interac_7 zero interac_9 interac_10 interac_11 interac_12 interac_13 ) omit ///
vertical  graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap)) yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6)
graph export "$outputs/ES/Baseline_Export5/Matching/Figures/`x'_full_c`y'_cut_noETFE.png", replace
}
} 
   
 
   
  
   
   
   
